//
//  LTCallManager.m
//  LTChat
//
//  Created by 俊松何 on 2017/8/1.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTCallManager.h"
#import "LTSendMessageHelper.h"
#import "LTClient.h"

@interface LTCallManager()

@end

@implementation LTCallManager

-(NSMutableArray *)callMangerdelegateArray
{
    if(_callMangerdelegateArray == nil)
    {
        _callMangerdelegateArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _callMangerdelegateArray;
}

/*
@param delegate 回调代理对象
*/
- (void)addDelegate:(id< LTCallManagerDelegate >)delegate
{
    [self.callMangerdelegateArray addObject:delegate];
}

- (void)removeDelegate:(id< LTCallManagerDelegate >)delegate
{
    [self.callMangerdelegateArray removeObject:delegate];
}

/**
 开始呼叫通话
 
 @param session  对方session
 @param completionBlock 完成的block
 */
- (void) startCallSession:(LTCallSession *)session
               completion:(void(^)(LTError *aError))completionBlock
{
    _isCall = YES;
    _chanleId = [LTClient sharedClient].currentUserId;
    
    LTMessageCmdType type;
    if(session.callType == LTCallTypeVideo)
    {
        type = LTMessageCmdTypeVideoCalling;
    }else {
        
        type = LTMessageCmdTypeAudioCalling;
    }
    
    
    LTMessageCmdBody *body = [[LTMessageCmdBody alloc] initWithType:type data:[LTClient sharedClient].currentUserId];
    
    LTMessage *message = [[LTMessage alloc] initWithConversationID:session.remoteUser.userId
                                                        fromUserId:[LTClient sharedClient].currentUserId
                                                          toUserId:session.remoteUser.userId
                                                          fromUser:[LTClient sharedClient].currentUser
                                                            toUser:session.remoteUser
                                                              type:LTMessageTypeCMD
                                                              body:body
                                                          chatType:LTChatTypeChat ext:nil];
    message.level = 3;
    
    [[LTSendMessageHelper helper] sendCmdMessage:message completion:^(LTMessage *message, LTError *error) {
        completionBlock(error);
    }];
    
}

/**
 结束通话
 
 @param session 回话session
 @param reson 结束原因
 */
- (void)endCall:(LTCallSession *)session
              reason:(NSString *)reson
          completion:(void(^)(LTError *aError))completionBlock
{
     _isCall = NO;
    _chanleId = nil;
    
    LTMessageCmdType type;
    if(session.callType == LTCallTypeAudio)
    {
        type = LTMessageCmdTypeEndAudioCall;
    }else
    {
        type = LTMessageCmdTypeEndVideoCall;
    }
    
    
    LTMessageCmdBody *body = [[LTMessageCmdBody alloc] initWithType:type data:session.callId];
    
    LTMessage *message = [[LTMessage alloc] initWithConversationID:session.remoteUser.userId
                                                        fromUserId:[LTClient sharedClient].currentUserId
                                                          toUserId:session.remoteUser.userId
                                                          fromUser:[LTClient sharedClient].currentUser
                                                            toUser:session.remoteUser
                                                              type:LTMessageTypeCMD
                                                              body:body
                                                          chatType:LTChatTypeChat ext:nil];
     message.level = 3;
    [[LTSendMessageHelper helper] sendCmdMessage:message completion:^(LTMessage *message, LTError *error) {
        completionBlock(error);
    }];

}

/**
 拒绝通话
 
 @param session 通话类型
 @param completionBlock 拒绝完成的block
 */
-(void)refuseCall:(LTCallSession *)session
       completion:(void(^)(LTError *aError))completionBlock
{
     _isCall = NO;
    
    LTMessageCmdType type;
    if(session.callType == LTCallTypeAudio)
    {
        type = LTMessageCmdTypeRefusedAudioCall;
    }else
    {
        type = LTMessageCmdTypeRefusedVideoCall;
    }
    
    LTMessageCmdBody *body = [[LTMessageCmdBody alloc] initWithType:type data:session.callId];
    LTMessage *message = [[LTMessage alloc] initWithConversationID:session.remoteUser.userId
                                                        fromUserId:[LTClient sharedClient].currentUserId
                                                          toUserId:session.remoteUser.userId
                                                          fromUser:[LTClient sharedClient].currentUser
                                                            toUser:session.remoteUser
                                                              type:LTMessageTypeCMD
                                                              body:body
                                                          chatType:LTChatTypeChat ext:nil];
     message.level = 3;
    [[LTSendMessageHelper helper] sendCmdMessage:message completion:^(LTMessage *message, LTError *error) {
        completionBlock(error);
    }];

}

/**
 接受通话

 @param session 通话session
 @param completionBlock 接受完成的block回调
 */
-(void)acceptCall:(LTCallSession *)session
       completion:(void(^)(LTError *aError))completionBlock
{
    _isCall = YES;
    
    LTMessageCmdType type;
    if(session.callType == LTCallTypeAudio)
    {
        type = LTMessageCmdTypeAcceptAudioCall;
    }else
    {
        type = LTMessageCmdTypeAcceptVideoCall;
    }
    
    LTMessageCmdBody *body = [[LTMessageCmdBody alloc] initWithType:type data:session.callId];
    LTMessage *message = [[LTMessage alloc] initWithConversationID:session.remoteUser.userId
                                                        fromUserId:[LTClient sharedClient].currentUserId
                                                          toUserId:session.remoteUser.userId
                                                          fromUser:[LTClient sharedClient].currentUser
                                                            toUser:session.remoteUser
                                                              type:LTMessageTypeCMD
                                                              body:body
                                                          chatType:LTChatTypeChat ext:nil];
    message.level = 3;
    [[LTSendMessageHelper helper] sendCmdMessage:message completion:^(LTMessage *message, LTError *error) {
        completionBlock(error);
    }];



}



@end
